namespace net.podhola.gtd.controllers

import System
import System.Configuration
import System.Globalization
import System.Web
import System.Web.UI
import System.Web.UI.WebControls
import Castle.MonoRail.Framework
import net.podhola.gtd.finances


/*,Scaffolding( Receipt )*/
[Layout("default")]
class ReceiptsController(GTDParentController):

	public override def Title():		
		return "Receipts"

	def FindReceipt( id as int ) as Receipt:
		PropertyBag["Receipt"] = Receipt.Find( id )
		return PropertyBag["Receipt"]

	public def Detail( id as int ):
		FindReceipt( id )

	public def Edit( id as int ):
		FindReceipt( id )
		
	public def Update( [DataBind("Receipt")] Receipt as Receipt ):
		accountid as int = Int32.Parse( Form[ "Receipt.ToAccount" ] )
		Receipt.ToAccount = Account.Find( accountid )
		amount as double = Double.Parse( Form[ "Receipt.Amount" ], NumberStyles.Currency )
		Receipt.Amount = amount
		Receipt.Save()
		RedirectToAction("Detail", { "id":Receipt.Id } )

	public def New():
		pass
		
	public def Create( Subject, Amount, ReceivedOn, ToAccount ):
		receipt = Receipt()
		accountid as int = Int32.Parse( ToAccount )
		receipt.ToAccount = Account.Find( accountid )
		
		amount as double = Double.Parse( Amount, NumberStyles.Currency )
		receipt.Amount = amount
		receipt.Subject = Subject
		receipt.ReceivedOn  = DateTime.Parse( ReceivedOn )			
		
		plannedIncome = ( Form[ "PlannedIncome.Id" ] + "" ) as string
		if plannedIncome.Length > 0:
			plannedIncomeId = Int32.Parse( plannedIncome )
			receipt.HasCoveredPlannedIncome = PlannedIncome.Find( plannedIncomeId )
		
		receipt.Create()

		PropertyBag["Receipt"] = receipt
		
	public def Delete( id as int ):
		FindReceipt( id ).Delete()
		RedirectToAction("Index" )
